package com.atguigu.tingshu.payment.strategy.impl;

import com.atguigu.tingshu.account.client.RechargeInfoFeignClient;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.account.RechargeInfo;
import com.atguigu.tingshu.model.payment.WeChatPayVo;
import com.atguigu.tingshu.payment.config.WxPayV3Config;
import com.atguigu.tingshu.payment.service.PaymentInfoService;
import com.atguigu.tingshu.payment.strategy.PaySubmitStrategy;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class RechargePaySubmitStrategy implements PaySubmitStrategy {

    @Autowired
    private RechargeInfoFeignClient rechargeInfoFeignClient;
    @Autowired
    private PaymentInfoService paymentInfoService;
    @Autowired
    private RSAAutoCertificateConfig autoCertificateConfig;
    @Autowired
    private UserInfoFeignClient userInfoFeignClient;

    @Autowired
    private WxPayV3Config wxPayV3Config;

    @Override
    public WeChatPayVo paySubmit(String paymentType, String orderNo) {
        log.info("RechargePaySubmitStrategy.....paySubmit...执行了");

        // 根据订单的编号查询充值记录
        Result<RechargeInfo> rechargeInfoResult = rechargeInfoFeignClient.findRechargeInfoByOrderNo(orderNo);
        RechargeInfo rechargeInfo = rechargeInfoResult.getData();

        // 保存交易信息
        paymentInfoService.savePaymentInfo(rechargeInfo);

        // 调用父类的方法请求微信支付平台的jsapi的下单接口
        // BigDecimal rechargeAmount = rechargeInfo.getRechargeAmount();
        WeChatPayVo weChatPayVo = this.requestJsapi(autoCertificateConfig, 1, wxPayV3Config, orderNo, userInfoFeignClient , "充值");

        return weChatPayVo;
    }

    @Override
    public String getpaymentType() {
        return SystemConstant.PAYMENT_TYPE_RECHARGE;
    }
}
